import 'package:fluent_ui/fluent_ui.dart';
import 'package:flutter/foundation.dart';
import 'package:get/get.dart';
import 'package:get/get_core/src/get_main.dart';
import '../common/link.dart';
import '../controller/system/menu_controller.dart';
import '../controller/system/user_controller.dart';
import '../init_context.dart';
import '../screens/settings.dart';
import '../theme.dart';
import '../utils/view.dart';
import 'package:bitsdojo_window/bitsdojo_window.dart';
class MainPage extends StatefulWidget {
  const MainPage({Key? key}) : super(key: key);

  @override
  _MyHomePageState createState() => _MyHomePageState();
}
const String appTitle = 'Fluent UI Showcase for Flutter';
class _MyHomePageState extends State<MainPage>{
  bool value = false;

  MenuController menuController = Get.find<MenuController>();
  UserController userController = Get.find<UserController>();
  int index = 0;
  final settingsController = ScrollController();
  final viewKey = GlobalKey();
  @override
  Widget build(BuildContext context) {
    final appTheme = Get.find<AppTheme>();
    return GetBuilder<MenuController>(
        builder: (_) =>NavigationView(
          key: viewKey,
          appBar: NavigationAppBar(
            automaticallyImplyLeading: false,
            title: () {
              if (kIsWeb) {
                return const Align(
                  alignment: AlignmentDirectional.centerStart,
                  child: Text(appTitle),
                );
              }
              return  MoveWindow(
                child: Align(
                  alignment: AlignmentDirectional.centerStart,
                  child: Text(appTitle),
                ),
              );
            }(),
              actions: Row(
                // crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  const Spacer(),
                  const WindowButtons(),
                ],
              )
          ),
          pane: NavigationPane(
            selected: index,
            onChanged: (i) {
              setState(() => index = i);
            },
            size: const NavigationPaneSize(
              openMinWidth: 250.0,
              openMaxWidth: 320.0,
            ),
            header:GetBuilder<UserController>(
              builder: (_)=>const Center(
                child: ListTile(
                    leading: FlutterLogo(),
                    title: Text("admin")
                ),
              ),
            ) ,
            displayMode: appTheme.displayMode,
            indicator: () {
              switch (appTheme.indicator) {
                case NavigationIndicators.end:
                  return const EndNavigationIndicator();
                case NavigationIndicators.sticky:
                default:
                  return const StickyNavigationIndicator();
              }
            }(),
            items: menuController.menuList,
            footerItems: [
              PaneItemSeparator(),//widget.pane._changeTo(item);
              PaneItemAction(
                icon: const Icon(FluentIcons.settings),
                title: const Text('Settings'),
                  onTap: ()  {
                   /* viewKeyPane._changeTo(Settings(controller: settingsController));*/

                  }
              ),
              LinkPaneItemAction(
                icon: const Icon(FluentIcons.open_source),
                title: const Text('Source code'),
                link: 'https://github.com/bdlukaa/fluent_ui',
              ),
              PaneItemAction(
                  icon: const Icon(FluentIcons.device_off),
                  title: const Text('退出'),
                  onTap: ()  {
                    showDialog(
                      context: context,
                      builder: (_) {
                        return ContentDialog(
                          title: const Text('登出'),
                          content: const Text('你确定要登出么?'),
                          actions: [
                            FilledButton(
                              child: const Text('是'),
                              onPressed: () async{
                                await cleanStorage();
                                Navigator.pop(context);
                                Navigator.pushNamedAndRemoveUntil(context, '/login', ModalRoute.withName('/login'));
                              },
                            ),
                            Button(
                              child: const Text('否'),
                              onPressed: () {
                                Navigator.pop(context);
                              },
                            ),
                          ],
                        );
                      },
                    );

                  }),
            ],
          ),
          content: NavigationBody.builder(
              index: index,
              transitionBuilder: (child, animation) {
                return DrillInPageTransition(child: child, animation: animation);
              },
              itemBuilder:(context,index){
                return Settings(controller: settingsController);
              })
        )
    );
  }
  @override
  void initState() {
    //windowManager.addListener(this);
    super.initState();

  }

  @override
  void dispose() {
    //windowManager.removeListener(this);
    settingsController.dispose();
    super.dispose();

  }
}

class WindowButtons extends StatefulWidget {
  const WindowButtons({Key? key}) : super(key: key);

  @override
  _WindowButtonsState createState() => _WindowButtonsState();
}

class _WindowButtonsState extends State<WindowButtons> {
  void maximizeOrRestore() {
    setState(() {
      appWindow.maximizeOrRestore();
    });
  }

  @override
  Widget build(BuildContext context) {
    final appTheme = Get.find<AppTheme>();
    return
      Center(child:Row(
        children: [
          MinimizeWindowButton(),
          appWindow.isMaximized
              ? RestoreWindowButton(
            onPressed: maximizeOrRestore,
          )
              : MaximizeWindowButton(
            onPressed: maximizeOrRestore,
          ),
          CloseWindowButton(onPressed:(){
            closeWindow(context);
          }),
        ],
      ) );

  }
}
